<?php
class msektor extends CI_Model{
	var $sektor = "m_sektor";
	var $nilai = "0";

	function __construct()
	{
		parent::__construct();
	}
	
	function setData($id_sektor,$nama_sektor,$id_resort)
	{
		$this->id_sektor= $id_sektor;
		$this->nama_sektor= $nama_sektor;
		$this->id_resort= $id_resort;
	}
	
	function getList($page,$uri_segment){
		$query = $this->db->get($this->sektor, $page, $uri_segment);
		if($query->num_rows() > 0){
			foreach($query->result_array() as $row){
				$result[] = $row;
			}
			return $result;
		} else {
			return false;
		}	
	}
	
	function getComboList(){
		$strSql = "
		select kepolisian.id,kepolisian.name,kepolisian.id_induk
		from (
		  select rs.id_resort as id, rs.nama_resort as name,rs.id_resort as id_induk
		  from m_resort rs
		  union all
		  select sk.id_sektor as id, CONCAT('-- ', sk.nama_sektor) as name, sk.id_resort as id_induk
		  from m_sektor sk
		) kepolisian
		where kepolisian.id_induk <> 0
		order by kepolisian.id_induk,kepolisian.id asc
		";
		$query = $this->db->query($strSql);
		if($query->num_rows() > 0){
			foreach($query->result_array() as $row){
				$result[] = $row;
			}
			return $result;
		} else {
			return false;
		}	
	}
	
	function create()
	{	
		$this->db->order_by("id_sektor", "desc");
		$this->db->limit(1);
		$this->db->where("id_resort",$this->id_resort);
		$query = $this->db->get($this->sektor);
		$result = $query->result_array();
		if($result[0]['id_sektor'] == ""){
			$nilai = ($this->id_resort."01");
		}else{
			if($result[0]['id_sektor'] < "10"){
				$nilai = ($this->id_resort."0".substr($result[0]['id_sektor'], -2, 2)+1);
			}else{
				$nilai = ($this->id_resort.substr($result[0]['id_sektor'], -2, 2)+1);
			}
		}
		
		$arrayData = array(
			'id_sektor'=>$nilai,
			'nama_sektor'=>$this->nama_sektor,
			'id_resort'=>$this->id_resort
		);
		return $this->db->insert($this->sektor, $arrayData);
	}
	
	function update($id_sektor)
	{
		$arrayData = array(
			'nama_sektor'=>$this->nama_sektor,
			'id_resort'=>$this->id_resort
		);
		$this->db->where('id_sektor', $id_sektor);
		return $this->db->update($this->sektor, $arrayData);
	}
        
	function remove($id_sektor)
	{
		$this->db->where('id_sektor', $id_sektor);
		return $this->db->delete($this->sektor);
	}	
	
	function detail($id_sektor)
	{
		$this->db->where('id_sektor', $id_sektor);
		$query = $this->db->get($this->sektor);	
		return $query->result_array();
	}
	
	function getListSearch($name){
		$this->db->like('nama_sektor',$name);
		$query = $this->db->get($this->sektor);
		if($query->num_rows() > 0){
			foreach($query->result_array() as $row){
				$result[] = $row;
			}
			return $result;
		} else {
			return false;
		}	
	}
}
?>